package com.example.qxfw.qxxx.controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.common.entity.qx.ForecastInfo;
import com.example.qxfw.common.entity.qx.Weather;
import com.example.qxfw.qxxx.vo.YbInfoVo;
import com.example.qxfw.ybzz.service.ForecastInfoService;
import com.example.qxfw.ybzz.service.WeatherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(tags = "气象信息-城镇预报")
@RestController
@CrossOrigin
@RequestMapping("/UrbanForecastController")
public class UrbanForecastController {

    @Autowired
    private ForecastInfoService forecastInfoService;

    @Autowired
    private WeatherService weatherService;

    @ApiOperation("气象信息-城镇预报-获取天气信息")
    @GetMapping("/getWeather")
    public SysResult getWeather() {
        List<Weather> weathers = weatherService.getWeather();
        return SysResult.success(weathers, weathers.size());
    }

    @ApiOperation("气象信息-城镇预报-获取风力信息")
    @GetMapping("/getWind")
    public SysResult getWind() {
        List<Weather> weathers = weatherService.getWind();
        return SysResult.success(weathers, weathers.size());
    }

    @ApiOperation("气象信息-城镇预报-获取天气 信息")
    @GetMapping("/getWindd")
    public SysResult getWindd() {
        List<Weather> weathers = weatherService.getWindd();
        return SysResult.success(weathers, weathers.size());
    }

    @ApiOperation("气象信息-城镇预报-获取预报信息")
    @RequestMapping("/getForecastInfo")
    public SysResult getForecastInfo(String dateChar, String timeChar, Integer nTimes, String city) {
        System.out.println(dateChar);
        System.out.println(timeChar);
        System.out.println(nTimes);
        System.out.println(city);
        List<ForecastInfo> forecastInfoList = forecastInfoService.getForecastInfo(dateChar, timeChar, nTimes, city);

        ArrayList<YbInfoVo> listYb = new ArrayList<>();

        //得到天气现象的方法
        List<Weather> weather = weatherService.getWeather();
        //得到风力信息的方法
        List<Weather> wind = weatherService.getWind();
        //得到风向信息的方法
        List<Weather> windd = weatherService.getWindd();

        for (ForecastInfo forecastInfo : forecastInfoList) {

            List<Weather> weathers = new ArrayList<>();
            List<Weather> weathers1 = new ArrayList<>();
            List<Weather> weathers2 = new ArrayList<>();
            weather.forEach(obj -> {
                if (obj.getWeatherid().equals(forecastInfo.getWeather1().toString().length() == 1 ? "0" + forecastInfo.getWeather1().toString() : forecastInfo.getWeather1().toString())) {
                    weathers.add(obj);
                }
            });

            wind.forEach(obj -> {
                if (obj.getWeatherid().equals(forecastInfo.getWind1().toString().substring(0,forecastInfo.getWind1().toString().length() - 2).length() == 1 ? "0" + forecastInfo.getWind1().toString().substring(0, forecastInfo.getWind1().toString().length() - 2) : forecastInfo.getWind1().toString().substring(0, forecastInfo.getWind1().toString().length() - 2))) {
                    weathers1.add(obj);
                }
            });

            windd.forEach(obj -> {
                if (obj.getWeatherid().equals(forecastInfo.getWindd1().toString().length() == 1 ? "0" + forecastInfo.getWindd1().toString() : forecastInfo.getWindd1().toString())) {
                    weathers2.add(obj);
                }
            });
            YbInfoVo build = YbInfoVo.builder()
                    .stationName(forecastInfo.getStationname())
                    .weather(weathers.get(0).getWeather())
                    .temperature(forecastInfo.getMintemp() + "-" + forecastInfo.getMaxtemp())
                    .windDirection(weathers2.get(0).getWindd())
                    .windPower(weathers1.get(0).getWind())
                    .longitude(forecastInfo.getLongitude())
                    .latitude(forecastInfo.getLatitude())
                    .build();

            listYb.add(build);
        }
        return SysResult.success(listYb, listYb.size());
    }

    private SysResult getForecastInfo(String dateChar, String timeChar, Integer nTimes) {

        return null;
    }

    @ApiOperation("气象信息-城镇预报-获取预报最新时间")
    @GetMapping("/getMaxTime")
    public SysResult getMaxTime() {
        List<ForecastInfo> forecastInfos = forecastInfoService.getMaxTime();
        return SysResult.success(forecastInfos);
    }

}
